from flask import Blueprint, render_template, request
from applications.common.utils.rights import authorize
from applications.models import Fire
from applications.models import Rectification
from applications.schemas import FireOutSchema
from applications.schemas import RectificationOutSchema
from applications.common.utils.http import table_api
from applications.common.curd import model_to_dicts
from applications.common.helper import ModelFilter
from applications.common.utils.validate import xss_escape


admin_fire = Blueprint('adminFire', __name__, url_prefix='/admin/fire')


# 火灾预测
@admin_fire.get('/')
# @authorize("fire:info", log=True)
def fire_info():
    return render_template('admin/fire/fire.html')


#   用户分页查询
@admin_fire.get('/data')
# @authorize("admin:fire:main", log=True)
def data():
    # 获取请求参数
    id = request.args.get('id', type=int)
    Name = xss_escape(request.args.get('Name', type=str))
    Ring = xss_escape(request.args.get('Ring', type=str))

    # 查询参数构造
    mf = ModelFilter()
    if id:
        mf.exact(field_name="id", value=id)
    if Name:
        mf.contains(field_name="Name", value=Name)
    if Ring:
        mf.contains(field_name="Ring", value=Ring)


    # orm查询
    # 使用分页获取data需要.items
    fire = Fire.query.filter(mf.get_filter(model=Fire)).layui_paginate()
    count = fire.total
    # 返回api
    return table_api(data=model_to_dicts(schema=FireOutSchema, data=fire.items), count=count)



#   用户分页查询
@admin_fire.get('/recti')
# @authorize("admin:fire:recti", log=True)
def recti():
    # 获取请求参数
    id = request.args.get('id', type=int)
    Name = xss_escape(request.args.get('Name', type=str))
    People = xss_escape(request.args.get('People', type=str))
    Register = xss_escape(request.args.get('Register', type=str))
    Rect = xss_escape(request.args.get('Rect', type=str))

    # 查询参数构造
    mf = ModelFilter()
    if id:
        mf.exact(field_name="id", value=id)
    if Name:
        mf.contains(field_name="Name", value=Name)
    if People:
        mf.contains(field_name="People", value=People)
    if Register:
        mf.contains(field_name="Register", value=Register)
    if Rect:
        mf.contains(field_name="Rect", value=Rect)


    # orm查询
    # 使用分页获取data需要.items
    rectif = Rectification.query.filter(mf.get_filter(model=Rectification)).layui_paginate()
    count = rectif.total
    # 返回api
    return table_api(data=model_to_dicts(schema=RectificationOutSchema, data=rectif.items), count=count)
